package com.chezai.client.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;

import com.chezai.client.global.NetConst;
import com.orhanobut.logger.Logger;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by likunyang on 2017/10/27.
 */

public class NetworkUtils {
    /**
     * ping 网络
     *
     * @return
     */
    public static final boolean ping() {
        String result = null;
        try {
            String ip = NetConst.RELEASE_SERVER ? NetConst.SERVER_TCP_ADDRESS : NetConst.TEST_TCP_ADDRESS;// ping 的地址，可以换成任何一种可靠的外网
            Process p = Runtime.getRuntime().exec("ping -c 3 " + ip);// ping网址3次
            // 读取ping的内容，可以不加
            InputStream input = p.getInputStream();
            BufferedReader in = new BufferedReader(new InputStreamReader(input));
            StringBuffer stringBuffer = new StringBuffer();
            String content = "";
            while ((content = in.readLine()) != null) {
                stringBuffer.append(content);
            }
            Logger.t("PING ").d("result content : " + stringBuffer.toString());
            // ping的状态
            int status = p.waitFor();
            if (status == 0) {
                result = "success";
                return true;
            } else {
                result = "failed";
            }
        } catch (IOException e) {
            result = "IOException";
        } catch (InterruptedException e) {
            result = "InterruptedException";
        } finally {
            Logger.t("PING ").d("result = " + result);
        }
        return false;
    }

    /**
     * 热点开关是否打开
     *
     * @return
     */
    public static boolean isWifiApEnabled(Context context) {
        try {
            WifiManager mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            Method method = mWifiManager.getClass().getMethod("isWifiApEnabled");
            method.setAccessible(true);
            return (Boolean) method.invoke(mWifiManager);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 关闭WiFi热点
     */
    public static void closeWifiAp(final Context context) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                if (isWifiApEnabled(context)) {
                    try {
                        Method apConfiguration = wifiManager.getClass().getMethod("getWifiApConfiguration");
                        apConfiguration.setAccessible(true);
                        WifiConfiguration config = (WifiConfiguration) apConfiguration.invoke(wifiManager);
                        Method apEnabled = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
                        apEnabled.invoke(wifiManager, config, false);
                    } catch (NoSuchMethodException e) {
                        e.printStackTrace();
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

    }

    /**
     * 检查网络是否可用
     *
     * @param ctx
     * @return
     */
    public static boolean isNetworkAvaliable(Context ctx) {
        if (ctx == null) {
            return false;
        }
        ConnectivityManager connectivityManager = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null) {
            final NetworkInfo net = connectivityManager.getActiveNetworkInfo();
            return net != null && net.isAvailable() && net.isConnected();
        } else {
            return false;
        }
    }
}
